import router from "./router";

import { getToken ,setToken } from "./uilts/token";
import store from "./store";

router.beforeEach(async (to, from, next) => {
  if (getToken()){
    if (store.state.n.nickName){
      next()
    }else {
      await store.dispatch('getUserInfo')
      next()
    }
  }else{
    if (to.path === "/"){
      if (!to.query.token){
        location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx44a5da4d82bfa8cb&redirect_uri=https://marketing.aliyunhg.com/callback&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
      }
      if (to.query.token){
        setToken(to.query.token)
        await store.dispatch('getUserInfo')
        next()
      }
    }else {
      next('/')
    }
  }
})
